Machine Learning : Classifier / Logistic Regression ( part 5 )
We were learn all the stuff regrading the Machine learning .
For Video Lecture on Machine Learning : Decision Go at bottom or google codewithnilesh
Logistic Regression actually is Classifier which is used to classify the data in two part i.e. Binary devision like True and False . One Region Contain the All predicted True Values and other contained the Flase predicted values .The Line which Seprate the region is called Boundary of Logistic Regression.
The Linear Regressor Equation is
log(Y / Y -1 ) = b0 + b1 * X1 + b2 * X2 + b2 * X2 + b2 * X2
Step 1 : Data Preprocessing :
Initially Import all Libraries like Pandas , numpy , matlibplot pyplot & Sklearn
using the pandas take data and classify in to dependent and independent variable.
Code :
#it classify the dasts using function who will through ads suv car
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#import the data sets
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[: , [2,3]].values# which coloumwant to use to predict the result
Y = dataset.iloc[: ,:4].values
Code written by @Garry Raut
Step 2 : Split the data in Test and Train Set :
we used the Sklearns model selction Liberaries and Train_test_split class of this
Split the data in X_train , X_test , Y_train , Y_test
code :
#split the set to traning and test sets
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.2)
# test size should be displayed
Step 3: Scaling the data for easy to classify :
For this Scaling we use the StandradScalar class from Library Sklearn . preprocessing.
Creating Object of class , SX object and using the fit method we will fit in the training and test set .Now we Scale the data.
Step 4: Now we Logistic Regression :
so for Logistic Regression we need the Logistic Class from the library sklearn . Linear model . we create object name ' Classifier ' to acces the class and fit to the model .Now we create an model .
Code:
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
# random state we get same result
classifier.fit(X_train,Y_train)
# fit the regession in train set
#prediction vector
Y_pred = classifier.predict(X_test)
step 5 : Confusion Matrix :
This matrix is used to differentiate between test result and Predicted result . so we idea of efficiency of model
Code :
#Confusion matrix contanied te train set prediction
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test,Y_pred)
Step 6 : Visualization of data
Code :
#visulization data seprate
from matplotlib.colors import ListedColormap
X_set, Y_set = X_train, Y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop =
X_set[:, 0].max() + 1, step =0.01),
np.arange(start = X_set[:, 1].min() - 1, stop =
X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array
([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('purple','green' )))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_set)):
plt.scatter(X_set[Y_set == j, 0], X_set[Y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
plt.title('Logistic Regression CodeWithNilesh')
plt.xlabel('CodeWithNilesh-Age')
plt.ylabel('CodeWithNilesh-Salary')
plt.legend()
plt.show()
Output :
Comments
Post a Comment